package com.bytz.modules.cms.productLine.service.impl;

import com.bytz.common.exception.BytzBootException;
import com.bytz.common.system.base.service.impl.AbstractCrudService;
import com.bytz.modules.cms.productLine.entity.ProductLine;
import com.bytz.modules.cms.productLine.entity.ProductLineLevel;
import com.bytz.modules.cms.productLine.mapper.ProductLineLevelMapper;
import com.bytz.modules.cms.productLine.service.IAdminProductLineLevelService;
import com.bytz.modules.cms.productLine.service.IProductLineLevelLimitService;
import com.bytz.modules.cms.productLine.service.IProductLineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @BelongsProject: cms-backend
 * @BelongsPackage: com.bytz.modules.cms.productLine.service.impl
 * @Author: dst
 * @CreateTime: 2022-10-27  15:32
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class AdminProductLineLevelServiceImpl extends AbstractCrudService<ProductLineLevelMapper, ProductLineLevel, ProductLineLevel> implements IAdminProductLineLevelService {

    @Autowired
    private IProductLineService productLineService;

    @Autowired
    private IProductLineLevelLimitService limitService;

    @Override
    protected ProductLineLevel insertSaveObject(ProductLineLevel saveRo) {
        if (lambdaQuery()
                .eq(ProductLineLevel::getProductLineId, saveRo.getProductLineId())
                .eq(ProductLineLevel::getLevelName, saveRo.getLevelName())
                .exists()) {
            throw new BytzBootException("已存在同名的等级，无法添加");
        }
        saveRo.setDiscountRatio(BigDecimal.ONE);
        saveRo.setEffectTime(LocalDateTime.now());
        save(saveRo);
        return saveRo;
    }

    @Override
    protected ProductLineLevel updateSaveObject(ProductLineLevel saveRo) {
        if (lambdaQuery()
                .eq(ProductLineLevel::getProductLineId, saveRo.getProductLineId())
                .eq(ProductLineLevel::getLevelName, saveRo.getLevelName())
                .ne(ProductLineLevel::getId, saveRo.getId())
                .exists()) {
            throw new BytzBootException("已存在同名的等级，无法保存");
        }
        updateById(saveRo);
        return saveRo;
    }

    @Override
    protected ProductLineLevel saveOthers(ProductLineLevel saveRo) {
        return saveRo;
    }

    @Override
    protected boolean deleteEntity(ProductLineLevel entity) {
        return removeById(entity.getId());
    }

    @Override
    protected void deleteOthers(ProductLineLevel entity) {
        limitService.removeByLevelId(entity.getId());
    }

    @Override
    public List<ProductLineLevel> getByLineId(String productLineId) {
        return lambdaQuery()
                .eq(ProductLineLevel::getProductLineId, productLineId)
                .list();
    }

    @Override
    public List<ProductLineLevel> getByLoginUser() {
        String lineId = productLineService.getLoginUserLine().map(ProductLine::getId).orElse(null);
        return lambdaQuery()
                .eq(lineId != null, ProductLineLevel::getProductLineId, lineId)
                .list();
    }
}
